package com.xiaohai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaohai.projo.*;
import com.xiaohai.mapper.ScoreMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaohai.projo.dto.ScoreDto;
import com.xiaohai.service.CourseService;
import com.xiaohai.service.ScoreService;
import com.xiaohai.service.StudentService;
import com.xiaohai.service.TeacherService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xiaohai
 * @since 2024-02-24
 */
@Service
public class ScoreServiceImpl extends ServiceImpl<ScoreMapper, Score> implements ScoreService {

    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private CourseService courseService;

    @Autowired
    private ScoreMapper scoreMapper;
    @Autowired
    private HttpServletRequest request;

    @Override
    public Long getByTeacherId(Integer id) {
        LambdaQueryWrapper<Score> scoreLambdaQueryWrapper = new LambdaQueryWrapper<>();
        scoreLambdaQueryWrapper.eq(id != null, Score::getTeaId, id);
        return super.count(scoreLambdaQueryWrapper);
    }

    @Override
    public Page<ScoreDto> myList(Integer currentPage, Integer pageSize, String studentName, String courseName, String teacherName, String name, Integer identity,String account) {
        Page<Score> page = new Page<>(currentPage, pageSize);
        Page<ScoreDto> list1 = null;
        if (identity == 1) {// 管理员账户
            list1 = scoreMapper.list(page, studentName, teacherName, courseName, name);
        }
        if (identity == 2) {// 学生账号
            list1 = scoreMapper.listbyStu(page, name, teacherName, courseName, account);
        }
        if (identity == 3) {// 教师账号
            list1 = scoreMapper.listbyTea(page, name, studentName, courseName, account);
        }
        return list1;
    }

    @Override
    public ScoreDto detail(String id) {
        ScoreDto scoreDto = scoreMapper.getById(id);
//        ScoreDto scoreDto = new ScoreDto();
//        Integer courceId = score.getCourceId();
//        Course course = courseService.getById(courceId);
//        Integer teaId = score.getTeaId();
//        Teacher teacher = teacherService.getById(teaId);
//        Integer stuId = score.getStuId();
//        Student student = studentService.getById(stuId);
//        scoreDto.setCourseName(course.getName());
//        scoreDto.setTeacherName(teacher.getName());
//        scoreDto.setStudentName(student.getName());
//        BeanUtils.copyProperties(score, scoreDto);
        return scoreDto;
    }

    @Override
    public boolean delete(Integer id) {
        boolean b = super.removeById(id);
        return b;
    }

    @Override
    public Boolean myUpdate(Score score) {
        boolean save = super.updateById(score);
        return save;
    }

    @Override
    public boolean add(Score score) {

        boolean save = super.save(score);
        return save;
    }

    @Override
    public Boolean batchAdd(List<Score> scores) {
        boolean b = super.saveBatch(scores);
        return b;
    }

    @Override
    public Boolean isEmptyByStudentId(Integer id) {
        LambdaQueryWrapper<Score> scoreLambdaQueryWrapper = new LambdaQueryWrapper<>();
        scoreLambdaQueryWrapper.eq(id != null, Score::getStuId, id);
        List<Score> list = super.list(scoreLambdaQueryWrapper);
        if (list.isEmpty()) {
            return true;
        }
        return false;
    }

    @Override
    public List<String> getNameList() {
        return scoreMapper.getNameList();
    }

    @Override
    public List<ScoreDto> exportList(Integer identity,String account) {
        List<ScoreDto> list1 = null;
        if (identity == 1) {// 管理员账户
            list1 = scoreMapper.exportList();
        }
        if (identity == 2) {// 学生账号
            list1 = scoreMapper.exportListByStu(account);
        }
        if (identity == 3) {// 教师账号
            list1 = scoreMapper.exportListByTea(account);
        }
        return list1;
    }
}
